﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.IO;
using iTextSharp;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using iTextSharp.text.html;
public partial class Main_Inventory_InventoryPrint_Default : System.Web.UI.Page
{
    Datautility dtcon = new Datautility();
    NumToWords dtnumtoword = new NumToWords();
    int tblCounter = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
            getItem();
        }
    }
    protected void GetData()
    {
        SqlParameter[] poparam = {new SqlParameter("@POID",Request.QueryString["poid"].ToString())};
        DataTable dtpo = dtcon.getdatausingprocedure("USP_ACC_GetPOREPORT", poparam);
        if (dtpo.Rows.Count > 0)
        {         
            lblcinno.Text = dtpo.Rows[0]["CINNO"].ToString();
            lblofficeadd.Text = dtpo.Rows[0]["billtolocation"].ToString();
            lbltinno.Text = dtpo.Rows[0]["TINNO"].ToString();
            lblvendorref.Text = dtpo.Rows[0]["VendorRef"].ToString();
            lblvendorCode.Text = dtpo.Rows[0]["VCODE"].ToString();
            lblpo.Text = dtpo.Rows[0]["poorder"].ToString();
            lblpoDate.Text = Convert.ToDateTime(dtpo.Rows[0]["PODATE"].ToString()).ToString("dd/MM/yyyy");
            lblVendorname.Text = dtpo.Rows[0]["VendorName"].ToString();
            lblvendorAddress.Text = dtpo.Rows[0]["VendorAddress"].ToString();
            lblRevisionNo.Text = dtpo.Rows[0]["Revision_no"].ToString();
            lblcurrency.Text = dtpo.Rows[0]["currency"].ToString();
            lbltermofdelivery.Text = dtpo.Rows[0]["Termofdelivery"].ToString();
            lblvendortinno.Text = dtpo.Rows[0]["vendortinno"].ToString();
            lblcontactPerson.Text = dtpo.Rows[0]["contactperson"].ToString();

            //lblbilltolocation.Text = dtpo.Rows[0]["billtolocation"].ToString(); 
            //lblshiptolocation.Text = dtpo.Rows[0]["shiptolocation"].ToString();

            lblbilltolocation.Text = "Communications Test Design India Pvt. Ltd, 15,Sec-6, IMT Manesar, Gurgaon, PinCode-122051, Phone No-0124-4748777, Fax No-0124-4748731, Email ID-vishnuchit.vats@ctdi.in, Website-www.ctdi.in";
            lblshiptolocation.Text = "Communications Test Design India Pvt. Ltd, 15,Sec-6, IMT Manesar, Gurgaon, PinCode-122051, Phone No-0124-4748777, Fax No-0124-4748731, Email ID-vishnuchit.vats@ctdi.in, Website-www.ctdi.in";
            lblDeliveryDate_Shedule.Text = Convert.ToDateTime(dtpo.Rows[0]["DeliveryDate"].ToString()).ToString("dd/MM/yyyy");
            lblAcceptanceCriteria.Text = dtpo.Rows[0]["AcceptanceCriteria"].ToString();
            lbltaxduties.Text = dtpo.Rows[0]["TaxDuties"].ToString();
            lblValidityofPO.Text= dtpo.Rows[0]["ValidityOfPO"].ToString();
            lblFreight.Text= dtpo.Rows[0]["Freight"].ToString();
            lblInsurance.Text= dtpo.Rows[0]["Insurance"].ToString();
            lblModeOfDispatch.Text= dtpo.Rows[0]["ModeOfDispatch"].ToString();
            lblWarranty.Text= dtpo.Rows[0]["Warranty"].ToString();
            lblhandlingpack.Text= dtpo.Rows[0]["Handling_Packing"].ToString();
            lblAnySpecialCondition.Text= dtpo.Rows[0]["AnySpecialCondition"].ToString();
            lblfreightfwd.Text = dtpo.Rows[0]["FreightFWD"].ToString();
            lblimportacno.Text = dtpo.Rows[0]["ImportAC_No"].ToString();
            lblRemarks.Text = dtpo.Rows[0]["Remarks"].ToString();
            lblpreparedBy.Text = dtpo.Rows[0]["Preparedby"].ToString();
            prepaireddesignationby.Text = dtpo.Rows[0]["PreparedDesignationby"].ToString();
            lblcheckedby.Text= dtpo.Rows[0]["checkedby"].ToString();
            lblcheckeddesignationby.Text= dtpo.Rows[0]["checkedDesignationby"].ToString();
            lblapprovedby.Text= dtpo.Rows[0]["approvedby"].ToString();
            lblapproveddesignationby.Text= dtpo.Rows[0]["approvedDesignationby"].ToString();          

            if (dtpo.Rows[0]["Revision_date"].ToString() == "1/1/1900 12:00:00 AM" || dtpo.Rows[0]["Revision_date"].ToString() == "")
            {
                lblrevisiondate.Text= "N/A";
            }
            else
            {
                if (dtpo.Rows[0]["Revision_no"].ToString() != "0")
                {
                    lblrevisiondate.Text = Convert.ToDateTime(dtpo.Rows[0]["Revision_date"].ToString()).ToString("dd/MM/yyyy");
                }
                else
                {
                    lblrevisiondate.Text = "N/A";
                }
            }
            lbltotal.Text = Convert.ToDouble(dtpo.Rows[0]["grandtotal"].ToString()).ToString("N2");
            if (!string.IsNullOrEmpty(lblcurrency.Text))
            {
                lbltotalinword.Text = dtnumtoword.ToWords(Convert.ToDouble(lbltotal.Text), lblcurrency.Text);
            }
            if (dtpo.Rows[0]["paymentmode"].ToString() == "0")
            {
                lblcashcredit.Text = "Advance";
            }
            else if (dtpo.Rows[0]["paymentmode"].ToString() == "1")
            {
                lblcashcredit.Text = dtpo.Rows[0]["creditday"].ToString() + " Days Net";
            }
            else if (dtpo.Rows[0]["paymentmode"].ToString() == "2")
            {
                lblcashcredit.Text = "Cash On Delivery";
            }
            else if (dtpo.Rows[0]["paymentmode"].ToString() == "3")
            {
                lblcashcredit.Text = "No Payment Required.";
            }
            string SqlStaus = "Select status from Acc_pomain where Poid=" + Request.QueryString["poid"].ToString();
            DataTable Dt = dtcon.getdata(SqlStaus);
            if (Dt.Rows.Count > 0)
            {                
                if (Dt.Rows[0]["status"].ToString() == "8")
                {
                    lblStatus.Visible = false;
                }
                if (Dt.Rows[0]["status"].ToString() == "7" || Dt.Rows[0]["status"].ToString() == "10")
                {
                    lblStatus.Text = "[Reject]";
                }
                if (Dt.Rows[0]["status"].ToString() == "4" || Dt.Rows[0]["status"].ToString() == "6" || Dt.Rows[0]["status"].ToString() == "9")
                {
                    lblStatus.Text = "[For Revision]";
                }
            }
            string SqlStauspo = "Select status from Acc_pomain where Poid=" + Request.QueryString["poid"].ToString();
            DataTable Dtpo = dtcon.getdata(SqlStauspo); 
            if (Dtpo.Rows[0]["status"].ToString() == "8")//Approved by the Admin
            {
                tblCheckedBy.Visible = true;
                tblPreparedBy.Visible = true;
                tblapprovedBy.Visible = true;
            }
            if (Dtpo.Rows[0]["status"].ToString() == "5")//Approved By the Finance.
            {
                tblCheckedBy.Visible = true;
                tblPreparedBy.Visible = true;
                tblapprovedBy.Visible = false;
            }
            if (Dtpo.Rows[0]["status"].ToString() == "3")//Approved By the Manager.
            {
                tblCheckedBy.Visible = false;
                tblPreparedBy.Visible = true;
                tblapprovedBy.Visible = false;
            }
        }
    }
    //protected void ADDROW()
    //{
    //    tblCounter++;
    //    HtmlTableRow htmlrow = new HtmlTableRow();

    //    HtmlTableCell tblcell1 = new HtmlTableCell();
    //    tblcell1.Width = "4%";
    //    Label lblsrno = new Label();
    //    lblsrno.ID = "lblsrno" + tblCounter.ToString();
    //    tblcell1.Controls.Add(lblsrno);

    //    HtmlTableCell tblcell2 = new HtmlTableCell();
    //    tblcell2.Width = "10%";
    //    Label lblItemCode=new Label();
    //    lblItemCode.ID = "lblItemCode" + tblCounter.ToString();
    //    tblcell2.Controls.Add(lblItemCode);

    //    HtmlTableCell tblcell3 = new HtmlTableCell();
    //    tblcell3.Width = "13%";
    //    Label lblAltitemcode=new Label();
    //    lblAltitemcode.ID = "lblAltitemcode" + tblCounter.ToString();
    //    tblcell3.Controls.Add(lblAltitemcode);

    //    HtmlTableCell tblcell4 = new HtmlTableCell();
    //    tblcell4.Width = "13%";
    //    Label lblMPN=new Label();
    //    lblMPN.ID = "lblMPN" + tblCounter.ToString();
    //    tblcell4.Controls.Add(lblMPN);

    //    HtmlTableCell tblcell5 = new HtmlTableCell();
    //    tblcell5.Width = "30%";
    //    Label lblItemdesc=new Label();
    //    lblItemdesc.ID = "lblItemdesc" + tblCounter.ToString();
    //    tblcell5.Controls.Add(lblItemdesc);

    //    HtmlTableCell tblcell6 = new HtmlTableCell();
    //    tblcell6.Width = "5%";
    //    Label lblqty=new Label();
    //    lblqty.ID = "lblqty" + tblCounter.ToString();
    //    tblcell6.Controls.Add(lblqty);

    //    HtmlTableCell tblcell7 = new HtmlTableCell();
    //    tblcell7.Width = "5%";
    //    Label lblunit=new Label();
    //    lblunit.ID = "lblunit" + tblCounter.ToString();
    //    tblcell7.Controls.Add(lblunit);

    //    HtmlTableCell tblcell8 = new HtmlTableCell();
    //    tblcell8.Width = "10%";
    //    Label lblpriceperunit=new Label();
    //    lblpriceperunit.ID = "lblpriceperunit" + tblCounter.ToString();
    //    tblcell8.Controls.Add(lblpriceperunit);

    //    HtmlTableCell tblcell9 = new HtmlTableCell();
    //    tblcell9.Width = "10%";
    //    Label lblamount=new Label();
    //    lblamount.ID = "lblamount" + tblCounter.ToString();
    //    tblcell9.Controls.Add(lblamount);
        
    //    htmlrow.Cells.Add(tblcell1);
    //    htmlrow.Cells.Add(tblcell2);
    //    htmlrow.Cells.Add(tblcell3);
    //    htmlrow.Cells.Add(tblcell4);
    //    htmlrow.Cells.Add(tblcell5);
    //    htmlrow.Cells.Add(tblcell6);
    //    htmlrow.Cells.Add(tblcell7);
    //    htmlrow.Cells.Add(tblcell8);
    //    htmlrow.Cells.Add(tblcell9);
        
    //    tblItem.Rows.Add(htmlrow);
    //    hiditem.Value = tblCounter.ToString();
    //}
    protected void getItem()
    {
        SqlParameter[] poitem = { new SqlParameter("@poid", Request.QueryString["poid"].ToString()) };
        DataTable dtitem = dtcon.getdatausingprocedure("USP_ACC_GetPOITEMREPORT", poitem);
        if (dtitem.Rows.Count > 0)
        {
            grditem.DataSource = dtitem;
            grditem.DataBind();
            //for (int i = 0; i < dtitem.Rows.Count; i++)
            //{
            //    ADDROW();                
            //}
            //for (int j = 1; j <= dtitem.Rows.Count; j++)
            //{
            //    Label lblSrno = (Label)tblItem.Rows[j].FindControl("lblsrno" + j.ToString());
            //    Label lblItemCode = (Label)tblItem.Rows[j].FindControl("lblItemCode" + j.ToString());
            //    Label lblAltitemcode = (Label)tblItem.Rows[j].FindControl("lblAltitemcode" + j.ToString());
            //    Label lblMPN = (Label)tblItem.Rows[j].FindControl("lblMPN" + j.ToString());
            //    Label lblItemdesc = (Label)tblItem.Rows[j].FindControl("lblItemdesc" + j.ToString());
            //    Label lblqty = (Label)tblItem.Rows[j].FindControl("lblqty" + j.ToString());
            //    Label lblunit = (Label)tblItem.Rows[j].FindControl("lblunit" + j.ToString());
            //    Label lblpriceperunit = (Label)tblItem.Rows[j].FindControl("lblpriceperunit" + j.ToString());
            //    Label lblamount = (Label)tblItem.Rows[j].FindControl("lblamount" + j.ToString());

            //    lblSrno.Text = j.ToString();
            //    lblItemCode.Text = dtitem.Rows[j - 1]["itemcode"].ToString();
            //    lblAltitemcode.Text = dtitem.Rows[j - 1]["AltItemCode"].ToString();
            //    lblMPN.Text = dtitem.Rows[j - 1]["MPNNO"].ToString();
            //    lblItemdesc.Text = dtitem.Rows[j - 1]["itemdesc"].ToString();
            //    lblqty.Text = dtitem.Rows[j - 1]["Itemqty"].ToString();
            //    lblunit.Text = dtitem.Rows[j - 1]["unitofm"].ToString();
            //    lblpriceperunit.Text = dtitem.Rows[j - 1]["rateExcltax"].ToString();
            //    lblamount.Text = dtitem.Rows[j - 1]["TotalAmount"].ToString();
            //}
        }
    }    
    protected void imgxl_Click(object sender, ImageClickEventArgs e)
    {
        //ConverToPdf();
        //CONVERTOTEXCEL();
    }
    protected void imgPdf_Click(object sender, ImageClickEventArgs e)
    {
        //ConverToPdf();
        CONVERTOTWORD();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        
    }
    private void CONVERTOTWORD()
    {
        //Label lblpo = (Label)grdpurchaseordermain.Rows[0].FindControl("lblpo");
        //System.Web.UI.WebControls.Image Image1 = (System.Web.UI.WebControls.Image)grdpurchaseordermain.Rows[0].FindControl("Image1");
        //Image1.ImageUrl = Server.MapPath("~/Company/Logo/065c8dfab2fb4c12b84125dbc76783f7.gif");
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", string.Format("attachment; filename=" + lblpo.Text + ".doc"));
        Response.ContentType = "application/ms-word";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        pnlporeport.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }
    private void CONVERTOTEXCEL()
    {        
        //Label lblpo = (Label)grdpurchaseordermain.Rows[0].FindControl("lblpo");
        //System.Web.UI.WebControls.Image Image1 = (System.Web.UI.WebControls.Image)grdpurchaseordermain.Rows[0].FindControl("Image1");
        //Image1.ImageUrl = Server.MapPath("~/Company/Logo/065c8dfab2fb4c12b84125dbc76783f7.gif");
        //Response.ClearContent();
        //Response.Buffer = true;
        //Response.AddHeader("content-disposition", string.Format("attachment; filename={0}",""+lblpo.Text+".xls"));
        //Response.ContentType = "application/ms-excel";
        //StringWriter sw = new StringWriter();
        //HtmlTextWriter htw = new HtmlTextWriter(sw);
        //grdpurchaseordermain.RenderControl(htw);
        //Response.Write(sw.ToString());
        //Response.End();
    }
    private void ConverToPdf()
    {
        Document document = new Document(PageSize.A4,10f,10f,10f,10f);
        Font noramlfont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);
        using (System.IO.MemoryStream MS = new System.IO.MemoryStream())
        {
            PdfWriter Writer = PdfWriter.GetInstance(document, Response.OutputStream);
            document.Open();
            PdfContentByte content = Writer.DirectContent;
            content.SetLineWidth(1.0f);
            content.MoveTo(10, document.Top - 110f);
            content.LineTo(585, document.Top - 110f);
            content.MoveTo(10, document.Top - 125f);
            content.LineTo(585, document.Top - 125f);
            content.MoveTo(10, document.Top - 235f);
            content.LineTo(585, document.Top - 235f);
           
            Rectangle rec = new Rectangle(document.PageSize);
            rec.Left += document.LeftMargin;
            rec.Right -= document.RightMargin;
            rec.Top -= document.TopMargin;
            rec.Bottom += document.BottomMargin;
            content.SetColorStroke(Color.BLACK);
            content.Rectangle(rec.Left, rec.Bottom, rec.Width, rec.Height);
            content.Stroke();         
            
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
            StringWriter sw = new StringWriter();
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            pnlporeport.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            pnlporeport.Style.Add("font-size", "0.0001px");
            pnlporeport.Style.Add("border-color", "#FFFFFF");        
            pnlporeport.RenderControl(hw);
            string PO = "<table border=0><tr><td>" + hw.InnerWriter + "</td></tr></table>";
            StringReader sr = new StringReader(PO);
            HTMLWorker htmlparser = new HTMLWorker(document);
            htmlparser.Parse(sr);
            document.Close();
            Response.Write(document);
            Response.End();
        }
        
        
        //Response.ContentType = "application/pdf";
        //Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
        //StringWriter sw = new StringWriter();       
        //Response.Cache.SetCacheability(HttpCacheability.NoCache);
        //HtmlTextWriter hw = new HtmlTextWriter(sw);        
        //pnlporeport.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
        //pnlporeport.Style.Add("font-size", "0.0001px");
        //pnlporeport.Style.Add("border-color", "#FFFFFF");        
        ////pnlporeport.Style.Add("border", "1px solid #808080");        
        //pnlporeport.RenderControl(hw);        
        //string PO = "<table border=0><tr><td>" + hw.InnerWriter + "</td></tr></table>";
        //StringReader sr = new StringReader(PO);
        //Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        //HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        
        //PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        //pdfDoc.Open();
        //htmlparser.Parse(sr);
        //pdfDoc.Close();
        //Response.Write(pdfDoc);
        //Response.End();
    }
}